import Vue from 'vue'
import Vuex from 'vuex'
import request_async from '../common/request_async.js';

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		hasLogin: false,
		newNotice: false,
		smsOpen: true,
		userInfo: {},
		upgrade: {infVersion: '', infVersionCode: ''},
	},
	mutations: {
		//更新state数据
		setStateAttr(state, param){
			if(param instanceof Array){
				for(let item of param){
					state[item.key] = item.val;
				}
			}else{
				state[param.key] = param.val;
			}
		},
		login(state, provider) {
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorage({//缓存用户登陆状态
			    key: 'userInfo',  
			    data: provider  
			}) 
			console.log('login', state.userInfo);
		},
		logout(state) {
			state.hasLogin = false;
			state.userInfo = {};
			uni.removeStorage({  
                key: 'userInfo'  
            })
		},
		upuser(state, provider){
			state.hasLogin = true;
			state.userInfo = provider;
			uni.setStorage({
			    key: 'userInfo',  
			    data: provider  
			})
			console.log('upuser', state.userInfo);
		}
	},
	actions: {
		async upuser(context, playload){
			console.log(playload)
			var params = {};
			if(playload.token){
				params.token =  playload.token;
			}
			var info = await request_async({
				name: 'user.info',
				method: 'GET',
				data: params
			});
			
			context.commit("upuser", info.data);
		},
		get_router(context, playload){
			var pages = getCurrentPages();
			var pagea = (pages[pages.length - 1]).route;
			return pagea;
		},
		//更新信息
		async upstate({state, commit}, playload){
			console.log('upstate playload', playload);
			commit('setStateAttr', {
				key: playload.key,
				val: playload.val
			})
			
		},
	}
})

export default store
